package com.example.jinyongjie.test_opengles;

import android.content.Context;
import android.opengl.GLSurfaceView;

import com.example.jinyongjie.test_opengles.objects.Mallet;
import com.example.jinyongjie.test_opengles.objects.Table;
import com.example.jinyongjie.test_opengles.programs.ColorShaderProgram;
import com.example.jinyongjie.test_opengles.programs.TextureShaderProgram;
import com.example.jinyongjie.test_opengles.util.Constants;
import com.example.jinyongjie.test_opengles.util.ShaderHelper;
import com.example.jinyongjie.test_opengles.util.TextResourceReader;
import com.example.jinyongjie.test_opengles.util.TextureHelper;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glVertexAttribPointer;
import static android.opengl.GLES20.glViewport;
import static android.opengl.Matrix.*;
/**
 * Created by jinyongjie on 17/3/8.
 */
public class MyRenderer implements GLSurfaceView.Renderer {
    private  final Context mContext;

    private final float[] mProjectionMatrix = new float[16];
    private final float[] mModelMatrix = new float[16];

    private TextureShaderProgram m_textureProgram;
    private Table m_table;
    private int m_texture;

    private ColorShaderProgram m_colorProgram;
    private Mallet m_mallet;


    public MyRenderer(Context context) {
        mContext = context;
    }


    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        glClearColor(0.0f,0.0f,0.0f,0.0f);

        m_table = new Table();
        m_textureProgram = new TextureShaderProgram(mContext);
        m_texture = TextureHelper.loadTexture(mContext, R.drawable.jyj);

        m_mallet = new Mallet();
        m_colorProgram = new ColorShaderProgram(mContext);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        glViewport(0, 0, width, height);
        perspectiveM(mProjectionMatrix, 0, 45, (float) width / (float) height, 1f, 10f);
        setIdentityM(mModelMatrix, 0);
        translateM(mModelMatrix, 0, 0f, 0f, -3f);
        rotateM(mModelMatrix, 0, -60f, 1f, 0f, 0f);
        final float temp[] = new float[16];
        multiplyMM(temp,0,mProjectionMatrix,0,mModelMatrix,0);
        System.arraycopy(temp, 0, mProjectionMatrix, 0, temp.length);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        glClear(GL_COLOR_BUFFER_BIT);

        m_textureProgram.useProgram();
        m_textureProgram.setUniforms(mProjectionMatrix, m_texture);
        m_table.bindData(m_textureProgram);
        m_table.draw();

        m_colorProgram.useProgram();
        m_colorProgram.setUniforms(mProjectionMatrix);
        m_mallet.bindData(m_colorProgram);
        m_mallet.draw(m_colorProgram);
    }
}
